# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Timo Teräs <timo.teras@iki.fi>
pkgname=grub
pkgver=2.06
pkgrel=17
pkgdesc="Bootloader with support for Linux, Multiboot and more"
url="https://www.gnu.org/software/grub/"
arch="all !s390x"
license="GPL-3.0-or-later"
depends="initramfs-generator"
triggers="grub.trigger=/boot"
makedepends="bison flex linux-headers xz-dev lvm2-dev fuse-dev
	automake autoconf libtool python3 freetype-dev font-unifont gettext-dev"
install="$pkgname.post-upgrade"
# strip handled by grub Makefiles, abuild strip breaks xen pv-grub
options="!check !strip"  # Cannot test boot loader.
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-mkfont
	$pkgname-mount
	$pkgname-bash-completion:bashcomp:noarch
	"

# currently grub only builds on x86*, aarch64 and ppc* systems
case "$CARCH" in
	x86)		flavors="efi bios";;
	x86_64)		flavors="efi bios xenhost xenhost_pvh";;
	aarch64|arm*)	flavors="efi";;
	mips*)		flavors="qemu_mips";;
	riscv*)		flavors="efi";;
	ppc*)		flavors="ieee1275"; makedepends="$makedepends powerpc-utils" ;;
	s390x)		flavors="emu" ;;
esac
for f in $flavors; do
	[ "$f" = "xenhost_pvh" ] && continue # xenhost_pvh shipped in the xenhost subpackage
	subpackages="$subpackages $pkgname-$f"
done

source="https://ftp.gnu.org/gnu/grub/grub-$pkgver.tar.xz
	update-grub
	grub-xen-host_grub.cfg

	0001-osdep-Introduce-include-grub-osdep-major.h-and-use-i.patch
	0002-osdep-linux-hostdisk-Use-stat-instead-of-udevadm-for.patch
	0003-build-Fix-build-error-with-binutils-2.36.patch
	0004-configure-Fix-misspelled-variable-BUILD_LDFAGS-BUILD.patch
	0005-RISC-V-Adjust-march-flags-for-binutils-2.38.patch
	0006-templates-Improve-initramfs-detection.patch
	0007-loader-arm64-linux-Remove-magic-number-header-field-.patch
	0008-loader-Drop-argv-argument-in-grub_initrd_load.patch
	0009-efi-Move-MS-DOS-stub-out-of-generic-PE-header-defini.patch
	0010-arm-linux-Unify-ARM-arm64-vs-Xen-PE-COFF-header-hand.patch
	0011-loader-arm64-linux-Account-for-COFF-headers-appearin.patch
	0012-arm64-efi-linux-Implement-LoadFile2-initrd-loading-p.patch
	0013-arm64-efi-linux-Ignore-FDT-unless-we-need-to-modify-.patch
	0024-riscv-binutils-2.40.patch
	xx-e2fsprogs-1.patch
	xx-e2fsprogs-2.patch

	0014-loader-Move-arm64-linux-loader-to-common-code.patch
	0015-RISC-V-Update-image-header.patch
	0016-RISC-V-Use-common-linux-loader.patch
	0017-Fix-grub-mkconfig-detection-of-linux-edge-virt.patch
	0018-ensure-grub-mkconfig-ignores-.apk-new-files.patch
	0019-prevent-harmless-LVM-warnings-from-grub-probe.patch
	0020-Handle-encrypted-zfs-root-partition.patch
	0021-Alpine-Linux-fixes-for-menu.patch
	0022-templates-fix-lang-locale-detection.patch
	0023-build-force-no-pie.patch
	fix-riscv64.patch
	fix-xfs-1.patch
	fix-xfs-2.patch
	grep-F.patch
	CVE-2021-3697-1.patch
	CVE-2021-3697-2.patch
	CVE-2021-3697-3.patch
	CVE-2021-3697-4.patch
	"

# secfixes:
#   2.06-r13:
#     - CVE-2021-3697
#   2.06-r0:
#     - CVE-2021-3418
#     - CVE-2020-10713
#     - CVE-2020-14308
#     - CVE-2020-14309
#     - CVE-2020-14310
#     - CVE-2020-14311
#     - CVE-2020-14372
#     - CVE-2020-15705
#     - CVE-2020-15706
#     - CVE-2020-15707
#     - CVE-2020-25632
#     - CVE-2020-25647
#     - CVE-2020-27749
#     - CVE-2020-27779
#     - CVE-2021-20225
#     - CVE-2021-20233

prepare() {
	default_prepare
	PYTHON=python3 sh ./autogen.sh
}

_build_flavor() {
	local flavor="$1"
	shift

	msg "Building grub for platform $flavor"
	mkdir -p "$srcdir"/build-$flavor
	cd "$srcdir"/build-$flavor
	$builddir/configure \
		--build=$CBUILD \
		--host=$CHOST \
		--target=$CTARGET \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--disable-nls \
		--disable-werror \
		"$@"
	make
}

build() {
	local f
	for f in $flavors; do
		case "$f" in
		bios)		CFLAGS="${CFLAGS/-fno-plt/}" \
				  _build_flavor $f --with-platform=pc;;
		efi)		_build_flavor $f --with-platform=efi --disable-efiemu;;
		xenhost)	_build_flavor $f --with-platform=xen;;
		xenhost_pvh)	CFLAGS="${CFLAGS/-fno-plt/}" \
				  _build_flavor $f --with-platform=xen_pvh --target=i386;;
		*)		_build_flavor $f --with-platform=$f;;

		esac
	done
}

_install_xen() {
	grub_dir=$(mktemp -d)
	cfg=$(mktemp)
	grub_memdisk=$(mktemp)

	mkdir -p $grub_dir/boot/grub
	echo 'normal (memdisk)/grub.cfg' > $cfg
	sed -e "s/@@PVBOOT_ARCH@@/$CARCH/g" \
		$srcdir/grub-xen-host_grub.cfg \
			> $grub_dir/grub.cfg
	tar -cf - -C $grub_dir grub.cfg > $grub_memdisk

	./grub-mkimage \
		-O $CARCH-xen \
		-c $cfg \
		-d ./grub-core ./grub-core/*.mod \
		-m $grub_memdisk \
		-o $pkgdir/grub-$CARCH-xen.bin

	rm -r "$grub_dir"
	rm "$cfg" "$grub_memdisk"
}

_install_xenpvh() {
	grub_dir=$(mktemp -d)
	cfg=$(mktemp)
	grub_memdisk=$(mktemp)

	mkdir -p $grub_dir/boot/grub
	echo 'normal (memdisk)/grub.cfg' > $cfg
	sed -e "s/@@PVBOOT_ARCH@@/$CARCH/g" \
		$srcdir/grub-xen-host_grub.cfg \
			> $grub_dir/grub.cfg
	tar -cf - -C $grub_dir grub.cfg > $grub_memdisk

	echo "About to run grub-mkimage in: $PWD"
	# Note: only i386-xen_pvh supported
	./grub-mkimage \
		-O i386-xen_pvh \
		-c $cfg \
		-d ./grub-core ./grub-core/*.mod \
		-m $grub_memdisk \
		-o $pkgdir/grub-i386-xen_pvh.bin

	rm -r "$grub_dir"
	rm "$cfg" "$grub_memdisk"
}

_install_flavor() {
	local flavor="$1"
	cd "$srcdir"/build-$flavor
	case $flavor in
	xenhost)	_install_xen;;
	xenhost_pvh)	_install_xenpvh;;
	*)	 	make DESTDIR="$pkgdir" install-strip;;
	esac
}

package() {
	# install BIOS & EFI version into the same directory
	# and overwrite similar files.
	for f in $flavors; do
		_install_flavor $f
	done

	rm -f "$pkgdir"/usr/lib/charset.alias
	# remove grub-install warning of missing directory
	mkdir -p "$pkgdir"/usr/share/locale

	mkdir -p "$pkgdir"/etc/default/
	cat >"$pkgdir"/etc/default/grub <<-EOF
	GRUB_DISTRIBUTOR="Alpine"
	GRUB_TIMEOUT=2
	GRUB_DISABLE_SUBMENU=y
	GRUB_DISABLE_RECOVERY=true
	EOF

	mkdir -p "$pkgdir"/usr/share/bash-completion
	mv "$pkgdir"/etc/bash_completion.d "$pkgdir"/usr/share/bash-completion/completions

	install -D -m755 "$srcdir"/update-grub "$pkgdir"/usr/sbin/update-grub
}

bios() {
	pkgdesc="$pkgdesc (BIOS version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-pc $subpkgdir/usr/lib/grub/
}

efi() {
	pkgdesc="$pkgdesc (EFI version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-efi $subpkgdir/usr/lib/grub/
}

xenhost() {
	pkgdesc="$pkgdesc (XEN host version)"
	mkdir -p $subpkgdir/usr/lib/grub-xen
	mv $pkgdir/*-xen.bin $subpkgdir/usr/lib/grub-xen/

	# pick up result from xenhost_pvh build as well
	mv $pkgdir/*-xen_pvh.bin $subpkgdir/usr/lib/grub-xen/
}

qemu_mips() {
	pkgdesc="$pkgdesc (QEMU MIPS version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-qemu_mips $subpkgdir/usr/lib/grub/
}

ieee1275() {
	pkgdesc="$pkgdesc (IEEE1275 version)"
	depends="$pkgname powerpc-utils"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-ieee1275 $subpkgdir/usr/lib/grub/
}

emu() {
	pkgdesc="$pkgdesc (EMU version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-emu $subpkgdir/usr/lib/grub/
}

mkfont() {
	pkgdesc="$pkgdesc (mkfont utility)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/bin
	mv $pkgdir/usr/bin/grub-mkfont $subpkgdir/usr/bin/
}

mount() {
	pkgdesc="$pkgdesc (mount utility)"
	depends=""
	mkdir -p $subpkgdir/usr/bin
	mv $pkgdir/usr/bin/grub-mount $subpkgdir/usr/bin/
}

bashcomp() {
	depends=""
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	amove usr/share/bash-completion/completions
}

sha512sums="
4f11c648f3078567e53fc0c74d5026fdc6da4be27d188975e79d9a4df817ade0fe5ad2ddd694238a07edc45adfa02943d83c57767dd51548102b375e529e8efe  grub-2.06.tar.xz
85b3c56670fb0aaaf831cc6d7ca45faeb68b844cb17cfd6f9b51c2473f0b153cfa7b26ff8bf00a88b2ed3e0ae69a1e8f735256e6cecc91e959169606e2abf887  update-grub
4e7394e0fff6772c89683039ccf81099ebbfe4f498e6df408977a1488fd59389b6e19afdbf0860ec271e2b2aea0df7216243dcc8235d1ca3af0e7f4d0a9d60a4  grub-xen-host_grub.cfg
377ecb3203bc19c792c3299cec5e678a8765b552a020e0f8cf29f881a9ed936fd00fb1eddde835daf98d9450078f5ab3a0236b6a6a8cd1ffc204e80739c7dc27  0001-osdep-Introduce-include-grub-osdep-major.h-and-use-i.patch
73dfe930925554cd750b37cb386e98f9b9ae0ccfde7d6edfda201e7a723173314523123fbd1aedaa83cf3649fc28550649a21709705b3a62b5d90914f58587c6  0002-osdep-linux-hostdisk-Use-stat-instead-of-udevadm-for.patch
b054e0fc91ff81980eebad07d1bafab6b5d11c48f544ef389a94e1da14a89aaed88ac1d0086ffc64d56f5b7bd5d9486bbf8f7c0fbba75bd34047ca677ed4ddf8  0003-build-Fix-build-error-with-binutils-2.36.patch
24ad940e33be72b75577d53017052b074f72a275804f8572a15ca0301fd742ce001459e8eee5d9ee90824837495d04060e494d414f241d9053931d40dafb15b9  0004-configure-Fix-misspelled-variable-BUILD_LDFAGS-BUILD.patch
91c52255be2668a2a16dba0ab6a89a2fe6c3b0cc71b20577feaadafeb7dd7f6274f4c2e20ed9c836b22cc88e46cf5528780493c73a5c9b865134b2529a20e45a  0005-RISC-V-Adjust-march-flags-for-binutils-2.38.patch
ad17a2af8fb73d23a1b4dc943bdcb6bbbfb02387f2fa3e4c05302ee5920d3c035b4c68004887d9f5997f07cea650c70b67e1f96442b32f62173b6f1722805bfb  0006-templates-Improve-initramfs-detection.patch
c0d7b9bb8876988c193c62d0dac05ddc9880c67d737551e6e169de30c5fd86060fa6c039e9959af2abbc55796aa7b5320bb28d1634c9a1238c2d61068acb5383  0007-loader-arm64-linux-Remove-magic-number-header-field-.patch
a8cb230c13e8e53f3a2b87f791350b1bc3e244e6d98d18297c5f71ba3b0c8e9c1cfdd288fdcdfd11119df676dbf16cb45b2dd4157474bf92cb42a176cbcb442e  0008-loader-Drop-argv-argument-in-grub_initrd_load.patch
4529b9808f5f496bbb2dde6aa3964a1545f49ca0d1c139ba60d135fc46dd464df67ac8e4572adfcbddb45dc8adf25efaa72ffefbaaaa45ba5df90e4da1d35e1c  0009-efi-Move-MS-DOS-stub-out-of-generic-PE-header-defini.patch
d2a5ec2a7d43b9be2af6194656be33a2fef0ffccacb03aec6f7386eaa623f98008922f943b77ec4941e9bcbc6793a4b95758e4e973c7341ae0224d6212e7b4e0  0010-arm-linux-Unify-ARM-arm64-vs-Xen-PE-COFF-header-hand.patch
ce80c16b8afac7dadfd0c16498466aa5c536663cf9ac98ad2aaf069137dcab28b6806609ff24285a33da70c3a993bcb70c51f1ad84b333c496c1ae9f3969eede  0011-loader-arm64-linux-Account-for-COFF-headers-appearin.patch
2f4f29a9ca0148f741b4f8540e6642d6cc51bb5ec3fa1510aef3730ad3a923698b8054c5590e8c96edde2a72f69e92de9931a6d14a8f19d5f69b87dd30011f33  0012-arm64-efi-linux-Implement-LoadFile2-initrd-loading-p.patch
8056810d6b2a39e476b6ffd47617ef208c415d37358a1fa1a8642a18af4ffaa8ada014bf87cbf2411e12461de00827faeedb06a797469c590c68d6d219824548  0013-arm64-efi-linux-Ignore-FDT-unless-we-need-to-modify-.patch
45db378f6f031faf7d062259f80fb1452d39d0a00fd111bc0b3e1e21fdc54735662017bff5c30eb2867d0b96ae28336df4bc0eaa5d1ab698d93c3282dddfc675  0024-riscv-binutils-2.40.patch
521c65af9d147959544aecea07adf59aba64bb1a10b6aa94e111d19ec748628069ad908f2d364a9aa910f671440ccde14fb5f874729ed39efced54e83ac3ed23  xx-e2fsprogs-1.patch
81d8a81f79b31263bef48333798a1f47385923d9956dc266931bc8b3235459ef3d20997192787b2be92ddbac7371a34f2241a493e048f1b567d454c2339d1027  xx-e2fsprogs-2.patch
3ca9c1595e70e83e26a13aa34dc94d753c95fefc399b1383f190045e1618756df5adcb94eda6b37c149bcc5cb967ee5c71d637144910f843df208578fbccee69  0014-loader-Move-arm64-linux-loader-to-common-code.patch
b69a24bf6d19fc262108d6064555754c8662ba1b8e4f68717419acafc18c5e04117c01bd40da3a724fbbc203ee423ee8440b5fc4f5b82e3e5f1529be82dc2e1c  0015-RISC-V-Update-image-header.patch
3ba2bc9da34638f650f28705e4c89131f6d80aef66ed7c3d3e017790b648ae38b94264c069fc20f94fbb9b7e595b70510decb76bedbe659ddfbb11c86350d93f  0016-RISC-V-Use-common-linux-loader.patch
6a631ba877542f2ba0837cc3f9bfcc8192dc735ec1c4185fd6c0e2a0246c08a3355665851335619b5ed136ac4c81f018635e833bbfe6c6f7e5a0f39940dc67c5  0017-Fix-grub-mkconfig-detection-of-linux-edge-virt.patch
97719bfd538b8b328a1b2a975bea4c836cea2b2c391d24f4ec66a4626c6ae2aecd4d5a343061d08174299db8556f07d6d0353485f7c2205f8d960ea2ea50e90e  0018-ensure-grub-mkconfig-ignores-.apk-new-files.patch
334e4559eb63ba3861b35d9502524dc6e6faa6af56c54554960854c293f9c524cddbb778f9bd7d0bd4f4e4ce8bfa5b9b0087445c2174b733c95640e44df38e6c  0019-prevent-harmless-LVM-warnings-from-grub-probe.patch
333231df2394a9257352b51712f9dd386193d6e52d591ad8456e54b4fb585e425ceb62843928d4e5e82dc17debe31eb7dc0ccca0cec7352303c718f58987e1ec  0020-Handle-encrypted-zfs-root-partition.patch
483d3aae4e3f943b203823be15fa225e943a7fc077f4c4cc59114fd21e16b237a2afd4499e7b6b373618c2551da1ed43c9ecddf9ec09103770d8726ad2b20241  0021-Alpine-Linux-fixes-for-menu.patch
d4e311ed2a849d9424eb215b55e217e7c0db80b54c7dc67679eb1ec81892e4dff77d4e837abbd17117c2ae6378a6bd06b0d57d2b4856d4045feafe1be43c2067  0022-templates-fix-lang-locale-detection.patch
552a0034b4f721d56b3ad7eb23b47b278690c219ba1a12fbf1f82eb88b03132c0b73fe11934f7ab26e7e2c819f1c20f06422ae2df26dbeaec8f0eaa3dfd4f03d  0023-build-force-no-pie.patch
e2a968890fb54f5a070e1de0a30ae6f0a71952ce5f607bfc43798852398142e342e1477b2b51d74f06ad83f49c35f3c81f6c8c9f9d012bc4a327c266a520124f  fix-riscv64.patch
ab487cb3f074e04d7bfcbb3bbd76109b3180ba1906baf6d25339496d1e9376673c924b9d11c60f0f8084bf85d5a766f5a5454ae736b75d433731a6100cfd5716  fix-xfs-1.patch
cfe20d2153375138ca4af2de2e2d076c410d3ecb0d5d8532e9a67a6387705e37013a56ea217df4d4756fff7120467d0f8b5ce60e72a367b4115b8285fb8e07f6  fix-xfs-2.patch
92aef47138e0ac344eb6e853cff45e811598000f04edf3eb06d1b904c8b73f7c63de4de9044a780e8d047cfbe05c1a508626e6dfc870448f5ec36ca8fbf68666  grep-F.patch
1ef0c87ab121a71453f9f34725ee33d21dfcc0918662a7483afc8213d773c7b663c6950c2120d54184ac833e8cb51312b1edc42d7649ec37529069d31c692a72  CVE-2021-3697-1.patch
0fd9e41fabd96ef119b9c24d7f380ad62060d7e3ebb58b832341adb4c682d2b10b2b5e36edc670460fe61e9355460b3af5a32a6c9d91f7fc9c9a2d53f52a15da  CVE-2021-3697-2.patch
bf9a07f739d2fabd133cdfd360d96f7960b940421130597dadf596bacf0489c64d229b5079ef7bc930e9d6a5c4ad823906340a1d608c4b37571235b976485829  CVE-2021-3697-3.patch
9c249a2e1067c25aa3b6a7c9daaf9fa1634c67ccc9a5b98b312fe8b3638ceb2531887cda9bd5c0a2e3c8846033ae9b2821a90a552ffe69335aee952e8eeaefdf  CVE-2021-3697-4.patch
"
